var needNewPagination_FORUMREAD = true;

var id = Utils.getUrlParam("id");

//帖子视图
app.controller('forumViewCtrl', ['$scope', '$sce', function ($scope, $sce) {
	
    var PAGESIZE = 10;
	
	var params = {
		id: id,
		pageNo: 1,
		pageCount: PAGESIZE,
	};
	
    //字符串以html形式翻译
    $scope.translateAsHtml = function (content) {
        return $sce.trustAsHtml(content);
    };
	
    $scope.evaluate = function (flag, id, index) {
        //1点赞，0吐槽
        var param = {
            "id": id,
            "evaluate": flag
        }
        AQ.callAQJSON('evaluateEntry.do',param, function(res) {
            $scope.$apply(function () {
                flag?$scope.childrens[index].evaluateGood++:$scope.childrens[index].evaluateBad++
            });
        }, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
    };
	
    var curPage = 1;
    $scope.public = function (id, content) {
        if(!content) return;
        var param = {
            "fatherId": id,
            "content": content
        }
        AQ.callAQJSON('createEntry.do',param, function (res) {
            $scope.$apply(function () {
                queryByPage(curPage);
            });
        }, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
    };
	
    $scope.pageOptions = {	//创建分页
        num_display_entries: 6,
        items_per_page: PAGESIZE,
        callback: function(currentPage) {
			curPage = currentPage + 1;
			if(currentPage == 0 && needNewPagination_FORUMREAD) {
				needNewPagination_FORUMREAD = false;
				return;
			}
			queryByPage(currentPage + 1);
		}
    };
	
    //小分页options
    $scope.childEntryOptions = function (index) {
        if($scope.childrens[index]){
            var param = $scope.childrens[index].childParam;
            $scope.childrens[index].options.callback = function(pageIndex) {
                param.pageNo = pageIndex;
                AQ.callAQJSON("getChildEntryById.do", param, function (res) {
                    var ss = [];
                    for(var i = 0; i < res.data.length; ++i) {
                        ss.push({
                            userName: res.data[i].userName,
							userImg: res.data[i].userImg,
                            createTime: res.data[i].createTime,
                            content: res.data[i].content
                        });
                    }
                    $scope.$apply(function () {
                        $scope.childrens[index].childEntryList = ss;
                    });
                }, function(err, code) {
					Utils.msgDialog(err + "["+ code +"]");
				});
            }
            //直接返回对象不行，返回angular自己的就没问题
            return $scope.childrens[index].options;
        }
    };
	
    //根据页码查询，起始为1
    function queryByPage(pageIndex) {
        params.pageNo = pageIndex;
        AQ.callAQJSON("readEntryById.do", params, function(res) {
            if (res.data.length > 0) {
                var father, childrensIndex = 0, childrens = [];
                if (pageIndex == 1) {
					id = res.data[0].id;
                    father = {
                        title: res.data[0].title,
                        categoryId: res.data[0].categoryId,
						category: DIC.ForumCate(res.data[0].categoryId),
                        createTime: res.data[0].createTime,
						hotDegree: res.data[0].hotDegree,
                        showCount: res.data[0].showCount,
						replyCount: res.data[0].replyCount,
                        userForumExp: res.data[0].userForumExp,
                        userName: res.data[0].userName,
						userImg: res.data[0].userImg,
                        content: res.data[0].content,
                    };
                    childrensIndex = 1;
                }
                for (var i = childrensIndex; i < res.data.length; ++i) {
                    var temp = [], tt = [];
                    for (var j = 0; j < res.data[i].childEntrys.childEntryList.length; ++j) {
                        temp.push({
                            userName: res.data[i].childEntrys.childEntryList[j].userName,
							userImg: res.data[i].childEntrys.childEntryList[j].userImg,
                            createTime: res.data[i].childEntrys.childEntryList[j].createTime,
                            content: res.data[i].childEntrys.childEntryList[j].content
                        });
                    }
                    var childLength = res.data[i].childEntrys.childEntryList.length;
                    childrens.push({
                        userName: res.data[i].userName,
						userImg: res.data[i].userImg,
                        userForumExp: res.data[i].userForumExp,
                        floor: (pageIndex - 1) * PAGESIZE + i + 1,
                        createTime: res.data[i].createTime,
                        content: res.data[i].content,
                        hasChildData: childLength > 0 ? true : false,
                        evaluateGood: res.data[i].evaluateGood,
                        evaluateBad: res.data[i].evaluateBad,
                        id: res.data[i].id,
                        childEntryList: temp,
                        childEntrySize: res.data[i].childEntrys.size,
                        childParam: {
                            id: res.data[i].id,
                            pageNo: "1",
                            pageCount: childLength
                        },
                        options: {
                            pageSize: childLength
                        }
                    });
                }

                if (pageIndex == 1) {
                    $scope.$apply(function () {
                        $scope.isFirst = true;
                        $scope.size = res.size;
                        $scope.father = father;
                        $scope.childrens = childrens;
                    });
                } else {
                    $scope.$apply(function () {
                        $scope.isFirst = false;
                        $scope.childrens = childrens;
                    });
                }
				//跳转到页面上部
				location.hash = "#search-forum";
            }
        }, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
    };
	
	queryByPage(1);	//初始查询第一页
	
	//右侧贴子入口
	refreshHotList();
	refreshNewList();
	
	function refreshHotList() {
		var param = {
			cateIds: null,
			startTime: null,
			endTime: null,
			pageNo: 1,
			pageCount: 10,
		};
		AQ.callAQJSON("getHotEntry.do", param, function(result) {
			var list = result.data;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var forumEntry = list[i];
				forumEntry.categoryId = DIC.ForumCate(forumEntry.categoryId);
				data.push(forumEntry);
			}
			$scope.$apply(function () {
				$scope.hotListData = data;
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}
	
	function refreshNewList() {
		var param = {
			cateIds: null,
			pageNo: 1,
			pageCount: 10,
		};
		AQ.callAQJSON("getNewEntry.do", param, function(result) {
			var list = result.data;
			var data = [];
			for (var i=0; i<list.length; i++) {
				var forumEntry = list[i];
				forumEntry.categoryId = DIC.ForumCate(forumEntry.categoryId);
				data.push(forumEntry);
			}
			$scope.$apply(function () {
				$scope.newListData = data;
			});
		}, function(err, code) {
			Utils.msgDialog(err + "["+ code +"]");
		});
	}


}]);



$(document).ready(function() {
	
	//点击搜索
    $("#search-forum").click(function() {
        var searchWords = $.trim($(".fm-txt").val());
		if(searchWords != "") {
			location.replace("forum-search.html?searchWords="+ searchWords);
		}
    });
	//点回车搜索
	$(".fm-txt").keydown(function(e) {
        if(e.keyCode == "13") {
			var searchWords = $.trim($(".fm-txt").val());
			if(searchWords != "") {
				location.replace("forum-search.html?searchWords="+ searchWords);
			}
		}
    });
	
	var ue = UE.getEditor('editor');
	
	$(".fa-tie-bt").click(function() {
        var content = UE.getEditor('editor').getContent();
		if($.trim(content).length == 0) {
			Utils.msgDialog("回复内容不能为空！");
			return;
		}
		var param = {
			fatherId: id,
			content: content,
		};
		AQ.callAQJSON("createEntry.do", param, function(result) {
			Utils.msgDialog("回复成功！");
			location.replace("forum-read.html?id="+ id);
    	}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
});

/*
点击回复跳到回复框位置
*/
function goReplyLocation() {
	location.hash = "#reply-box";
}

/*
显示子回复
*/
function showChildAns(dom) {
	var box = $(dom).parents(".bss-slide").find(".praise-cont");
	var replyBox = $(dom).parents(".bss-slide").find(".js-ajax");
	if(box.css("display") != "none" && replyBox.css("display") != "none") {
		box.slideUp(200);
		replyBox.hide();
	} else {
		box.slideDown(200);
		replyBox.show();
	}
}

/*
显示子回复框
*/
function showChildReply(dom) {
	var replyBox = $(dom).parents(".bss-slide").find(".js-ajax");
	if(replyBox.css("display") == "none") {
		replyBox.show();
	} else {
		replyBox.hide();
	}
}
